﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ConfigCreate.Controls
{
    /// <summary>
    /// CreateControlGroup.xaml 的交互逻辑
    /// </summary>
    public partial class CreateControlGroup : System.Windows.Controls.UserControl
    {
        internal static long group_id = 0;
        private bool is_init = true;

        /// <summary>
        /// 重置组ID生成
        /// </summary>
        public static void ResetGroupId()
        {
            group_id = 0;
        }

        /// <summary>
        /// 子项
        /// </summary>
        public ObservableCollection<CreateControlBasePanel> Items { get; set; } = new ObservableCollection<CreateControlBasePanel>();

        /// <summary>
        /// 
        /// </summary>
        public CreateControlGroup()
        {
            InitializeComponent();

            GroupId = System.Threading.Interlocked.Increment(ref group_id);
        }

        #region 属性

        /// <summary>
        /// 自动生成的组ID，按控件生成顺序递增
        /// </summary>
        public long GroupId { get; }

        /// <summary>
        /// 组排序索引，越小越靠前
        /// </summary>
        public int GroupIndex { get; set; } = int.MaxValue;

        /// <summary>
        /// 父容器Key
        /// </summary>
        public string? ParentKey { get; set; }

        /// <summary>
        /// 标题
        /// </summary>
        public string Header
        {
            get { return (string)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }
        /// <summary>
        /// 标题
        /// </summary>
        public static DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(string), typeof(CreateControlGroup), new FrameworkPropertyMetadata("杂项"));

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded
        {
            get { return (bool)GetValue(IsExpandedProperty); }
            set { SetValue(IsExpandedProperty, value); }
        }
        /// <summary>
        /// 是否展开
        /// </summary>
        public static DependencyProperty IsExpandedProperty = DependencyProperty.Register("IsExpanded", typeof(bool), typeof(CreateControlGroup), new FrameworkPropertyMetadata(true));

        #endregion

        /// <summary>
        /// 权限列表
        /// </summary>
        internal ConcurrentDictionary<RolePermissionType, HashSet<string>> RolePermission { get; } = new ConcurrentDictionary<RolePermissionType, HashSet<string>>();
        internal bool CheckRolePermission(RolePermissionType type, string? role_name)
        {
            if (string.IsNullOrEmpty(role_name))
                return true;
            if (!RolePermission.TryGetValue(type, out var tl))
                return true;
            return tl.Contains(role_name);
        }

        /// <summary>
        /// 添加控件
        /// </summary>
        /// <param name="panel">控件</param>
        public void AddControlPanel(CreateControlBasePanel panel)
        {
            Items.Add(panel);
        }

        /// <summary>
        /// 删除控件
        /// </summary>
        /// <param name="panel">控件</param>
        public void RemoveControlPanel(CreateControlBasePanel panel)
        {
            Items.Remove(panel);
        }

        /// <summary>
        /// 清除所有控件
        /// </summary>
        public void CelarControlPanel()
        {
            Items.Clear();
        }

        private void root_Loaded(object sender, RoutedEventArgs e)
        {
            if (is_init && string.IsNullOrEmpty(Header))
            {
                is_init = false;
                g_root.Children.Remove(expander);
                expander.Content = null;
                g_root.Children.Add(b_group);
                b_group.BorderThickness = new Thickness(1);
                b_group.CornerRadius = new CornerRadius(4);
            }
        }
    }
}
